package com.qdsg.ysg.doctor.ui.activity.mine;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseActivity;
import com.qdsg.ysg.doctor.base.BaseApplication;
import com.qdsg.ysg.doctor.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.response.SignBean;
import com.rest.response.UserInfoBean;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @Description: 签名设置
 * @Author: lzp
 * @CreateDate: 2022/5/10 16:31
 * @UpdateRemark: 更新说明
 */
public class SignatureSettingActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_unauthorized)
    TextView tvUnauthorized;
    @BindView(R.id.ll_authorized)
    LinearLayout llAuthorized;
    @BindView(R.id.tv_reset_signature)
    TextView tvResetSignature;
    @BindView(R.id.ll_unsigned)
    LinearLayout llUnsigned;
    @BindView(R.id.ll_signed)
    LinearLayout llSigned;
    @BindView(R.id.iv_signature)
    ImageView ivSignature;
    @BindView(R.id.btn_start_auth)
    TextView btnStartAuth;
    @BindView(R.id.btn_achieve)
    TextView btnAchieve;
    @BindView(R.id.tv_set_signature)
    TextView tvSetSignature;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_signature_setting;
    }

    @Override
    public void initData() {
        querySignStatus();
    }

    @Override
    protected void initViews() {
        tvTitle.setText("签名设置");
    }


    @OnClick({R.id.btn_back, R.id.tv_reset_signature, R.id.btn_start_auth, R.id.btn_achieve, R.id.tv_set_signature})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
            case R.id.btn_achieve:
                finish();
                break;
            case R.id.tv_reset_signature:
            case R.id.tv_set_signature:
                toSignature();
                break;
            case R.id.btn_start_auth:
                ToastUtil.showToast(mContext, "请先去小程序完成实名认证");
                break;
            default:
                break;
        }
    }

    public void querySignStatus() {
        showProgressDialog(mContext);
        RestProxy.getInstance().querySignStatus(BaseApplication.currentUserId, new Observer<UserInfoBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(UserInfoBean userInfoBean) {
                //已实名认证
                if ("0".equals(userInfoBean.data.realNameStatus)) {
                    tvUnauthorized.setVisibility(View.GONE);
                    llAuthorized.setVisibility(View.VISIBLE);
                    //签名为空 表示未签名
                    if (TextUtils.isEmpty(userInfoBean.data.signatureImg)) {
                        llUnsigned.setVisibility(View.VISIBLE);
                    } else {
                        llUnsigned.setVisibility(View.GONE);
                        llSigned.setVisibility(View.VISIBLE);
                        byte[] decodedString = Base64.decode(userInfoBean.data.signatureImg, Base64.DEFAULT);
                        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
                        ivSignature.setImageBitmap(decodedByte);
                        btnStartAuth.setVisibility(View.GONE);
                        btnAchieve.setVisibility(View.VISIBLE);
                        tvResetSignature.setVisibility(View.VISIBLE);
                    }
                }
                dismissProgressDialog();
            }

            @Override
            public void onError(Throwable e) {
                dismissProgressDialog();
            }

            @Override
            public void onComplete() {

            }
        });
    }

    //跳完h5页面签名
    public void toSignature() {
        RestProxy.getInstance().userSignature("https://ysg.com/caSignature", new Observer<SignBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(SignBean signBean) {
                Bundle bundle = new Bundle();
                bundle.putString("url", signBean.data.url);
                startActivityForResult(SignatureActivity.class, bundle, 2000);
            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError(mContext, e);
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == 2000) {
            querySignStatus();
        }
    }
}
